package com.example.sy.pojo;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.Data;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

//data 包含了tostring、get、set 等方法
@Data
public class user {
    //    设置 姓名、登陆密码
    public String username;
    public String password;
    public String avatar;
    public String name;
    public String role;

    //新增结果
    public Map<String,Object> addResult(int code) {
        Map<String,Object> map = new HashMap<>();
        //列举
        if (code == 1) {
            map.put("code", 200);
            map.put("msg", "success");
        } else if (code == 2) {
            map.put("code", 500);
            map.put("msg", "已有该账户,请重新注册");
        } else {
            map.put("code", 500);
            map.put("msg", "error");

        }
        return map;
    }

    //删除结果
    public Map<String,Object> deleteResult(int code) {
        Map<String,Object> map = new HashMap<>();
        //列举
        if (code == 1) {
            map.put("code", 200);
            map.put("msg", "success");
        } else {
            map.put("code", 500);
            map.put("msg", "error");

        }
        return map;
    }

    //登录结果(单设备登录)
    public Map<String, Object> loginResult(int size,String username,String password) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        //列举
        if (size > 0) {
            map.put("code", 200);
            map.put("msg", "success");
            data.put("token",this.createToken(username,password));
            //该用this 同在一个类里
            map.put("data", data);

        } else {
            map.put("code", 500);
            map.put("msg", "登录失败");

        }
        return map;
    }
    //生成token
    public String createToken(String username,String password){
        System.out.println(username);
        System.out.println(password);
        Calendar instance =Calendar.getInstance();
        //设置存活90秒
        instance.add(Calendar.SECOND,10000);
        String token= JWT.create().withClaim("username",username)
                .withClaim("password",password).withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256("wangs"));
        return token;
    }
    //判断是否存在
    public int analysis(String token){
        int status;
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("wangs")).build();
        //创建解码后的JWT对象
        DecodedJWT decodedJWT = jwtVerifier.verify(token); //参数是上一步生成的JWT字符串
    //  System.out.println("用户名: " + decodedJWT.getClaim("username").asString());
        if(decodedJWT.getClaim("username").asString()!=""&&decodedJWT.getClaim("password").asString()!=""){
            status=1;
        }
        else{
            status=0;
        }
        return status;
    }
    //获取信息
    public String getUsername(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("wangs")).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        return decodedJWT.getClaim("username").asString();
    }

}
